package com.steven.unittest;

import android.app.Application;
import android.content.Context;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by sulei on 2019/9/8.
 *
 * @author sulei
 */
public class AssetsUtil {

    public static String getAssetsFileContent(Context context, String assetsFileName) {
        String result           = null;
        InputStream inputStream = null;
        try {
            inputStream = context.getAssets().open(assetsFileName);
            int         length      = inputStream.available();
            byte[]      buffer      = new byte[length];
            inputStream.read(buffer);
            inputStream.close();
            result = new String(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(null != inputStream)
            {
                try {
                    inputStream.close();
                } catch (IOException e) {
                }
            }
        }
        return result;
    }

    public static File getSharedPrefsFile(Context context, String name)
    {
        File sharedPrefsDir = context.getDir("shared_prefs", Context.MODE_PRIVATE);

        // context.getDir得出来的dir是app_xxx形式的，这里处理一下

        String dirPath = sharedPrefsDir.getAbsolutePath();
        dirPath = dirPath.replace("app_shared_prefs", "shared_prefs");

        File sharedPrefsFile = new File(dirPath, name + ".xml");
        return sharedPrefsFile;
    }
}
